package com.teamall.web.tssy.controller;

import java.math.*;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.teamall.core.common.BaseController;
import com.teamall.core.common.CommonEnum;
import com.teamall.core.common.JModelAndView;
import com.teamall.core.common.Page;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.log.LogManager;
import com.teamall.web.tssy.db.po.TsSyResourcePO;
import com.teamall.web.tssy.db.po.TsSyRoleResourcePO;
import com.teamall.web.tssy.service.ITsSyResourceService;
import com.teamall.web.tssy.service.ITsSyRoleResourceService;
import com.teamall.web.tssy.vo.TsSyResourceTree;

/**
 * <Description>tssyroleresource管理 <br>
 * 
 * @author codeCreater <br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.teamall.web.tssy.controller <br>
 */

@Controller
@RequestMapping("/tssy/tsSyRoleResource")
public class TsSyRoleResourceController extends BaseController {

    private static final LogManager logger = LogManager.getLogger(TsSyRoleResourceController.class);

    @Autowired
    private ITsSyRoleResourceService tsSyRoleResourceService;
    @Autowired
    private ITsSyResourceService tsSyResourceService;
    
    @RequestMapping("index")
    public String index(Model model) {
        // ///////
        // TODO 根据业务场景，进行条件分支跳转、设置页面默认值等

        // ///////

        return "/page/tssy";
    }

    @RequestMapping("queryRecordByPage")
    public ModelAndView queryRecordByPage(TsSyRoleResourcePO record,
            Page page,HttpServletRequest request) throws BaseAppException {
    	logger.info("queryRecordByPage by record begin...record=[{0}]", record);
    	ModelAndView mv = new JModelAndView("tssy/tsSyRoleResourceList",request);
    	//将dwz分页参数转成Page对象
//    	Page page = dwzToPage(dwzPage);
    	Page<TsSyRoleResourcePO> resultPage = tsSyRoleResourceService.selectByArgAndPage(record, page);
    	mv.addObject("resultPage", resultPage);
    	mv.addObject("record", record);
    	
    	return mv;
    }

    @RequestMapping("initAdd")
    public ModelAndView initAdd(TsSyRoleResourcePO record,HttpServletRequest request) throws BaseAppException {
        logger.info("initAdd add begin...record=[{0}]", record);
        ModelAndView mv = new JModelAndView("tssy/tsSyRoleResourceAdd", request);
        return mv;
    }
    
    @RequestMapping(value="add",method=RequestMethod.POST)
    public ModelAndView add(TsSyRoleResourcePO record) throws BaseAppException {
        logger.info("add record begin...record=[{0}]", record);
        tsSyRoleResourceService.add(record);
        return ajaxDoneSuccess(this.operSuccess);
    }

    @RequestMapping(value="update",method=RequestMethod.PUT)
    public ModelAndView update(TsSyRoleResourcePO record) throws BaseAppException {
        logger.info("update record begin...record=[{0}]", record);
        tsSyRoleResourceService.update(record);
        return ajaxDoneSuccess(this.operSuccess);
    }

    @RequestMapping(value = "delete",method=RequestMethod.DELETE)
    public ModelAndView delete(@RequestParam(value = "id",required = true) String id) throws BaseAppException {
        logger.info("delete record begin...id=[{0}]", id);
        tsSyRoleResourceService.updateStatus(id);
        return ajaxDoneSuccess(operSuccess);
    }

    @RequestMapping("qryRecordInfo")
    public ModelAndView qryRecordInfo(@RequestParam(value = "id",
            required = true) String id,HttpServletRequest request) throws BaseAppException {
    	logger.info("qryRecordInfo by id begin...id=[{0}]", id);
    	ModelAndView mv = new JModelAndView("tssy/tsSyRoleResourceAdd",request);
    	TsSyRoleResourcePO record = tsSyRoleResourceService.selectByPrimaryKey(id);
    	mv.addObject("record", record); 
    	return mv;  
    }

    @RequestMapping(value="loadTreeData",method=RequestMethod.GET)
    @ResponseBody
    public List<TsSyResourceTree> loadTreeData(@RequestParam(value = "pid",
            required = true) String pid,@RequestParam(value = "roleId",
            required = true) String roleId)throws BaseAppException {
    	TsSyResourcePO queryPo = new TsSyResourcePO();
    	queryPo.addNewConditions("isDel", CommonEnum.ISDEL_F.getCode().toString());
    	List<TsSyResourcePO> list = tsSyResourceService.selectByArg(queryPo);
    	TsSyRoleResourcePO roleResource = new TsSyRoleResourcePO();
		roleResource.addNewConditions("roleId", roleId);
    	List<TsSyRoleResourcePO> listRR = tsSyRoleResourceService.selectByArg(roleResource);
    	List<TsSyResourceTree> treeList = queryTreeByPid(pid,roleId,list,listRR);
    	return treeList;
    }
    
    private List<TsSyResourceTree> queryTreeByPid(String pid,String roleId,List<TsSyResourcePO> list,List<TsSyRoleResourcePO> listRR){
    	List<TsSyResourceTree> treeList = new ArrayList<>();
			if(!CollectionUtils.isEmpty(list)){
				for(TsSyResourcePO po:list){
					if(pid.equals(po.getPid())){
						TsSyResourceTree tree = new TsSyResourceTree();
						if(!CollectionUtils.isEmpty(listRR)){
							for(TsSyRoleResourcePO tsrrp:listRR){
								if(tsrrp.getResourceId().equals(po.getId())){
									tree.setChecked(true);
									break;
								}else{
									tree.setChecked(false);
								}
							}
						}else{
							tree.setChecked(false);
						}
						tree.setId(po.getId());
						tree.setName(po.getResourceName());
						tree.setPid(po.getPid());
						tree.setChildren(queryTreeByPid(po.getId(),roleId,list,listRR));
						treeList.add(tree);
					}
				}
			}
    	return treeList;
    }
    
    /*@RequestMapping(value="saveRoleResource",method=RequestMethod.GET)
    @ResponseBody
    public String saveRoleResource(@RequestParam(value = "checked",
            required = true) boolean checked,@RequestParam(value = "resourceId",
            required = true) String resourceId,@RequestParam(value = "roleId",
            required = true) String roleId)throws BaseAppException{
    	TsSyRoleResourcePO roleResource = new TsSyRoleResourcePO();
		roleResource.addNewConditions("roleId", roleId);
		roleResource.addNewConditions("resourceId", resourceId);
    	List<TsSyRoleResourcePO> listRR = tsSyRoleResourceService.selectByArg(roleResource);
    	if(checked){
    		if(!CollectionUtils.isEmpty(listRR)){
    		}else{
    			TsSyRoleResourcePO record = new TsSyRoleResourcePO();
    			record.setResourceId(resourceId);
    			record.setRoleId(roleId);
    			tsSyRoleResourceService.add(record);
    		}
    	}else{
    		if(!CollectionUtils.isEmpty(listRR)){
    			TsSyRoleResourcePO record = listRR.get(0);
    			tsSyRoleResourceService.delete(record);
    		}else{
    		}
    	}
    	return "";
    }*/
}
